package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneCommonService;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteSpringUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IIndustryService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.KiteIndustryConfig;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @Description: TODO
*
* @author lyx
* @date 2020年12月20日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr
*/
@Service
public class Industry5GAttrHandler extends AbstractAttrHandler{

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Autowired
    private IQuerySceneCommonService querySceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if (KiteStringUtils.isEmpty(commonAttr.getValue())) {
            LocalAttrHelper.setSpecialAttrValue(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr, this.getClass().getSimpleName());
        }

        // 如果产品线有值则设置细类下拉值
        KiteAttr productLineAttr = querySceneCommonService.getCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), LocalKeyConsts.PRODUCT_LINE).getResultObject();
        if (Objects.isNull(productLineAttr)) {
            productLineAttr = querySceneCommonService.getCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), LocalKeyConsts.PRODUCT_LINE_NEW).getResultObject();
        }
        // 选定产品线后联动出细分重点产品
        if (Objects.nonNull(productLineAttr) && KiteStringUtils.isNotEmpty(productLineAttr.getValue())) {
            IIndustryService industryService = KiteSpringUtils.getBeanHighPriority(IIndustryService.class);
            List<KiteIndustryConfig> kiteIndustryList = industryService.listSubIndustry(productLineAttr.getValue());
            List<KiteAttrValue> kiteAttrValues = kiteIndustryList.stream().map(kiteIndustry -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(kiteIndustry.getIndustryCode());
                kiteAttrValue.setAttrValueName(kiteIndustry.getIndustryName());
                kiteAttrValue.setAttrValueDesc(kiteIndustry.getIndustryName());
                return kiteAttrValue;
            }).collect(Collectors.toList());
            commonAttr.setAttrValues(KiteBeanUtils.copyPropsForList(kiteAttrValues, StaticAttrDO.class));
        }
        LocalAttrHelper.hidden5GCommonAttr(KiteWebContext.getContextNew().getCurrentSceneInstId(), KiteWebContext.getContext().getCustId(), commonAttr);
    }

}
